package cn.jy.nvr.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class TimeUtils {
	
	
	public  static String getIntegerTime(String time){
		Date nowtime=null;
		try {
			 nowtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar now=Calendar.getInstance();
		now.setTime(nowtime);
		int hour = now.get(Calendar.HOUR_OF_DAY);
		int min = now.get(Calendar.MINUTE);
//	    if(min%10>=5){
//	       min=(min/10+1)*10;
//	    }else {
//	    	min=(min/10)*10;
//		}
	    if (hour>12) {
			hour=hour-12;
		}
//	    String strMin=null;
//	    String strHour=String.valueOf(hour);
//	    if(min==0){
//	      strMin="00";
//	    }else if(min==60){
//	     strMin="00";
//	     strHour=String.valueOf(hour+1);
//	    }else{
//	    strMin=String.valueOf(min);
//	    }
	    String strHour = new DecimalFormat("00").format(hour);
		String strMin = new DecimalFormat("00").format(min);
	    String intergerTime=strHour+":"+strMin;
 		return intergerTime;
	}
	public static String getTime(String dateTime ){
		Date date=null;
		String time=null;
		try {
			date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar now=Calendar.getInstance();
		now.setTime(date);
		int hour = now.get(Calendar.HOUR_OF_DAY);
		int min = now.get(Calendar.MINUTE);
		String formatHour = new DecimalFormat("00").format(hour);
		String formatMin = new DecimalFormat("00").format(min);
		time=formatHour+":"+formatMin;
		return time;
	}
	
	public static   String  getInputSecondsOuptMin (String seconds) {
		Integer intseconds = Integer.valueOf(seconds);
		if(intseconds<60){
			return String.valueOf(intseconds)+"秒";
		}else{
			Integer  min =intseconds/60;
			Integer  s= intseconds%60;
			if (s==0) {
				return  String.valueOf(min)+"分";
			}
			return  String.valueOf(min)+"分"+String.valueOf(s)+"秒";
		}
	}
	public static   String  getInputDateOutSeconds (String date, String beforeDate ) {
		Date dateFormat = DateTools.getInstance().stringFormat(date, "yyyy-MM-dd HH:mm:ss");
		Date beforeDateFormat = DateTools.getInstance().stringFormat(beforeDate, "yyyy-MM-dd HH:mm:ss");
		long   s  =   ( dateFormat.getTime()- beforeDateFormat.getTime())/1000;
		return String.valueOf(s);
	}
	public static   String  minToSeconds (String min ) {
		String[] arraymin = min.split(":");
		String minStr=arraymin[0];
		String sStr=arraymin[1];
		Integer minInt=Integer.valueOf(minStr)*60;
		Integer sInt=Integer.valueOf(sStr);
		sInt=minInt+sInt;
		return String.valueOf(sInt);
	}
	
	 public static List<Date> findDates(Date dBegin, Date dEnd) {
         List<Date> lDate = new ArrayList<Date>();
         lDate.add(dBegin);
         Calendar calBegin = Calendar.getInstance();
     // 使用给定的 Date 设置此 Calendar 的时间
         calBegin.setTime(dBegin);
         Calendar calEnd = Calendar.getInstance();
     // 使用给定的 Date 设置此 Calendar 的时间
           calEnd.setTime(dEnd);
         // 测试此日期是否在指定日期之后
         while (dEnd.after(calBegin.getTime()))  {
          // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
              calBegin.add(Calendar.DAY_OF_MONTH, 1);
              lDate.add(calBegin.getTime());
         }
         return lDate;
    }
	 
	 public static  String getAgeFromBirthTime(String birthDay){
		 
		  String strs[] = birthDay.trim().split("-");  
	        int selectYear = Integer.parseInt(strs[0]);  
	        int selectMonth = Integer.parseInt(strs[1]);  
	        int selectDay = Integer.parseInt(strs[2]);  
	        // 得到当前时间的年、月、日  
	        Calendar cal = Calendar.getInstance();  
	        int yearNow = cal.get(Calendar.YEAR);  
	        int monthNow = cal.get(Calendar.MONTH) + 1;  
	        int dayNow = cal.get(Calendar.DATE);  
	  
	        // 用当前年月日减去生日年月日  
	        int yearMinus = yearNow - selectYear;  
	        int monthMinus = monthNow - selectMonth;  
	        int dayMinus = dayNow - selectDay;  
	  
	        int age = yearMinus;// 先大致赋值  
	        if (yearMinus < 0) {// 选了未来的年份  
	            age = 0;  
	        } else if (yearMinus == 0) {// 同年的，要么为1，要么为0  
	            if (monthMinus < 0) {// 选了未来的月份  
	                age = 0;  
	            } else if (monthMinus == 0) {// 同月份的  
	                if (dayMinus < 0) {// 选了未来的日期  
	                    age = 0;  
	                } else if (dayMinus >= 0) {  
	                    age = 1;  
	                }  
	            } else if (monthMinus > 0) {  
	                age = 1;  
	            }  
	        } else if (yearMinus > 0) {  
	            if (monthMinus < 0) {// 当前月>生日月  
	            } else if (monthMinus == 0) {// 同月份的，再根据日期计算年龄  
	                if (dayMinus < 0) {  
	                } else if (dayMinus >= 0) {  
	                    age = age + 1;  
	                }  
	            } else if (monthMinus > 0) {  
	                age = age + 1;  
	            }  
	        }  
		 return String.valueOf(age);
	 }
	 
	 
	 public static String getCountDate(String beforeDate, String nowDate) {
		      Date before=new Date();
			  Date now=new Date();
			try {
				  before = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(beforeDate);
				  now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(nowDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		    long nd = 1000 * 24 * 60 * 60;
		    long nh = 1000 * 60 * 60;
		    long nm = 1000 * 60;
		    long ns = 1000;
		    // 获得两个时间的毫秒时间差异
		    long diff = now.getTime() - before.getTime();
		    // 计算差多少天
		    long day = diff / nd;
		    // 计算差多少小时
		    long hour = diff % nd /nh+day*24;
		    // 计算差多少分钟
		    long min = diff % nd % nh / nm;
		    // 计算差多少秒//输出结果
		    long sec = diff % nd % nh % nm / ns;
		    
		    return   hour + "小时" + min + "分钟"+sec+"秒";
		}
}
